package com.ruoyi.web.sso.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.ruoyi.common.utils.StringUtils;
import org.codehaus.jettison.json.JSONObject;

import java.io.*;
import java.util.*;

public class FileUtil {

    public static void main(String[] args) throws Exception {
        String destPath="E:\\2";

        new Modify(destPath);

    }

    //复制文件夹的方法,开始的时候是两个文件夹
    public static void copyFile(String source,String destin) throws Exception //在这里直接抛出各种异常,就不一一处理了
    {
        File path1=new File(source);
        File path2=new File(destin);

        //如果源目录不存在,那就不用复制了,
        if(path1.exists())
        {
            //Create destination folder,如果目标目录不存在,就创建目标目录,因为没有目录文件复制不过去的
            if(!path2.exists())
            {
                path2.mkdirs();
            }

            //取得源目录下面的所有文件和文件夹
            File[] items=path1.listFiles();
            FileInputStream fis=null;
            FileOutputStream fos=null;

            //取得所有文件和文件夹之后,遍历处理,如果是文件,就复制文件,如果是文件夹,则递归文件夹下面的文件和文件夹
            for(File item:items)
            {
                //如果是文件才进行复制
                if(item.isFile())
                {
                    //输入输出流的两个常用构造函数,其中在用来了一个字段File.separator,先用输入流读取文件,然后用输出流写文件到目标位置,完成复制功能
                    fis=new FileInputStream(item);

                    String fileName = item.getName();
                    String newFileName=fileName.substring(0,fileName.lastIndexOf(".") )+"Resp"+fileName.substring(fileName.lastIndexOf(".") );

                    fos=new FileOutputStream(path2+File.separator+newFileName);
                    byte[] b=new byte[1024];
                    for(int i=0;(i=fis.read(b))!=-1;)
                    {
                        fos.write(b,0,i);
                        fos.flush();
                    }

                    //close the Stream关闭资源啊,什么异常处理的就不写,自己补上吧
                    fos.close();
                    fis.close();

                }
                //如果是文件夹,递归文件夹
                else
                {
                    copyFile(item.getPath(),path2+File.separator+item.getName());
                }
            }
        }
        else
        {
            System.out.println("source path:"+source+" is not exists!");
        }
    }
}

class Modify {
    Set set = new HashSet();
    private String path;

    public Modify(String path) {
        // 操作目录。从该目录开始。该文件目录下及其所有子目录的文件都将被替换。
        this.path = path;
        operation();
    }

    private void operation() {
        File file = new File(path);
        opeationDirectory(file);
    }

    public void opeationDirectory(File dir) {

        File[] files = dir.listFiles();
        for (int i = 0; i < files.length; i++) {
            File f = files[i];
            if (f.isDirectory())
                // 如果是目录，则递归。
                opeationDirectory(f);
            if (f.isFile()) {
                String name = f.getParentFile().getAbsolutePath()+"\\"+ f.getName();
                System.out.println(name);
                String v = readFileByLines(name);

                String line = getName_99(v);

                BufferedWriter bw = null;   // 输出配置文件
                try {
                    bw = new BufferedWriter(new FileWriter(name));
                    bw.write(line);
                    bw.flush();
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }


    public  String readFileByLines(String fileName) {
        FileInputStream file = null;
        BufferedReader reader = null;
        InputStreamReader inputFileReader = null;
        String content = "";
        String tempString = null;
        try {
            file = new FileInputStream(fileName);
            inputFileReader = new InputStreamReader(file, "utf-8");
            reader = new BufferedReader(inputFileReader);
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                content += tempString;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return content;
    }

    public String getName(String v){
        String v_1 = "http://dungeonrelics.online";
        String v_2 = "https://dungeonrelics.online/dung_statics";
        Map<String, String> jsonMap = JSON.parseObject(v, new TypeReference<HashMap<String, String>>() {
        });
        String image = jsonMap.get("image");
        image = image.replaceFirst(v_1,v_2)+".webp";
        jsonMap.put("image",image);
        String appNames = jsonMap.get("attributes");
        List<Map<String, String>> list = (List<Map<String, String>>) JSONArray.parse(appNames);
        for (int i = 0; i < list.size() ; i++) {
            for(Map.Entry entry : list.get(i).entrySet()){
                if (i == 0 && StringUtils.equals((String)entry.getKey(),"value")){
                    String value = list.get(i).get("value").replaceFirst("bg-","");
                    list.get(i).put("value",value);
                }else {
                    String value = list.get(i).get("value");
                    String[] v2 = value.split("-");
                    if (v2.length > 1){
                        String v4 = v2[0].substring(0, 1).toUpperCase() + v2[0].substring(1);
                        String v3 = v2[1].substring(0, 1).toUpperCase() + v2[1].substring(1);
                        list.get(i).put("value",v4 +" "+ v3);
                    }else {
                        String v4 = v2[0].substring(0, 1).toUpperCase() + v2[0].substring(1);
                        list.get(i).put("value",v4);
                    }
                }
                String trait_type = list.get(i).get("trait_type").substring(0, 1).toUpperCase() + list.get(i).get("trait_type").substring(1);
                list.get(i).put("trait_type",trait_type);
            }
        }
        String  attributes = JSON.toJSON(list).toString();
        jsonMap.put("attributes","attributes_1");
        JSONObject json = new JSONObject(jsonMap);
        String count =  JSON.parse(json.toString()).toString().replace("\"attributes_1\"",attributes);
        return count ;
    }

    public String getName_99(String v){
        Map<String, String> jsonMap = JSON.parseObject(v, new TypeReference<HashMap<String, String>>() {});
        String data = jsonMap.get("data");
        Map<String, String> jsonMap_1 = JSON.parseObject(data, new TypeReference<HashMap<String, String>>() {});
        String hits = jsonMap_1.get("hits");
        List<Map<String, String>> list = (List<Map<String, String>>) JSONArray.parse(hits);
        String [] count = new String[list.size()];
        List stringList = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            stringList.add(list.get(i).get("holderAddress"));
        }
        set.addAll(stringList);
        String [] count_1 = (String[]) set.toArray(count);
        System.out.println(set.size());
        return Arrays.toString(count_1);
    }

}
